import pyglet
from pyglet.gl import *

import cocos
from cocos.actions import *

from cocos.layer import *
from cocos.menu import *
from cocos.scene import *
from cocos.scenes.transitions import *
from cocos.sprite import Sprite
from cocos.director import director

import Box2D as box2d 

import game
from res import Images


class MenuScene(Scene):
    "A menu scene"
    def __init__(self,paused=False):
        Scene.__init__(self)
        self.paused = paused
        
        self.menuOverlay = MenuOverlay(self.paused)
        self.menuBG = MenuBG()
        
        self.add(self.menuOverlay, z=3)
        self.add(self.menuBG, z=2)
    
    def on_new_game(self):
        self.remove(self.menuOverlay)
        self.menuOverlay = None
        director.replace(game.GameScene())#ZoomTransition(game.GameScene(),duration=0.1))
        
    def on_quit(self):
        director.pop()
        
        
class MenuOverlay(MultiplexLayer):
    "Menu Overlay, can be run without MenuScene"
    def __init__(self,paused=False):

        self.bg = MenuOverlayBG()
        self.mainMenu = MainMenu(paused)
        self.optionsMenu = OptionsMenu(paused)
        self.mainMenu.parent = self.optionsMenu.parent = self
        
        MultiplexLayer.__init__(self, self.mainMenu, self.optionsMenu)
        self.add(self.bg,z=-1)
        
    def update(self):
        pass # prevent error when being self.update by someone else
    

        

class MainMenu(Menu):
    
    def __init__(self, paused=False):
        Menu.__init__(self)
        
        self.paused = paused

        self.font_title['font_size'] = 60
        self.font_title['color'] = (0,0,0,255)
        self.font_item['font_size'] = 30
        self.font_item['color'] = (0,0,0,255)
        self.font_item_selected['font_size'] = 35
        self.font_item_selected['color'] = (100,100,100,255)

        #self.menu_valign = self.menu_halign = CENTER

        self.title = "~Paused~" if self.paused else "BoxFight!"
            
        
        items = []
        
        
        items.append(MenuItem('Resume', self.on_resume) if self.paused 
                     else MenuItem('New Game', self.on_new_game) )
        
        items.append(MenuItem('Options', self.on_options ))
        items.append(MenuItem('Scores', self.on_scores ))
        
        items.append(MenuItem('End Game', self.on_end_game) if self.paused  
                     else MenuItem('Quit', self.on_quit))

        self.create_menu( items, shake(), shake_back() )
        #self.create_menu( items, (shake()|zoom_in()), (shake_back()|zoom_out()) )

    def on_scores( elf):
        print "scores not implemented yet"
        pass#self.parent.switch_to( 2 )

    def on_options(self):
        self.parent.switch_to(1)

    def on_quit(self):
        if isinstance(self.parent.parent, game.GameScene):
            gameScene = self.parent.parent
            gameScene.on_resume()
        elif isinstance(self.parent.parent, MenuScene):
            menuScene = self.parent.parent
            menuScene.on_quit()
        
    def on_resume(self):
        if isinstance(self.parent.parent, game.GameScene):
            gameScene = self.parent.parent
            gameScene.on_resume()
            
    def on_new_game( self ):
        if isinstance(self.parent.parent, MenuScene):
            menuScene = self.parent.parent
            menuScene.on_new_game()
    
    def on_end_game(self):
        if isinstance(self.parent.parent, game.GameScene):
            gameScene = self.parent.parent
            gameScene.on_end_game()
            
    
    
        
        
        
class OptionsMenu(Menu):
    def __init__(self, paused=False):
        Menu.__init__(self, "Options")
        
        self.paused=paused
        
        self.font_title['font_size'] = 60
        self.font_title['color'] = (0,0,0,255)
        self.font_item['font_size'] = 30
        self.font_item['color'] = (0,0,0,255)
        self.font_item_selected['font_size'] = 35
        self.font_item_selected['color'] = (100,100,100,255)
        
        #self.menu_valign = self.menu_halign = CENTER
        
        self.items = [
                        ToggleMenuItem("Show FPS: ", self.on_show_fps, director.show_FPS),
                        MenuItem("Back", self.on_back)
                            ]
        
        self.create_menu(self.items, shake(), shake_back())
            
    def on_show_fps(self, value):
        director.show_FPS = value

    def on_back(self):
        self.parent.switch_to(0)
        
    def on_quit(self):
        self.parent.switch_to(0)
        

class MenuBG(ColorLayer):
    def __init__(self):
        ColorLayer.__init__(self, 147, 140, 255, 200)


class MenuOverlayBG(Layer):
    def __init__(self):
        Layer.__init__(self)
        self.image= Sprite(Images.menu_overlay)
        pos = director.window.get_size()
        self.image.position = (pos[0]/2,pos[1]/2)
        self.add(self.image)

    def draw( self ):
        glPushMatrix()
        self.transform()
        glPopMatrix()
        

        
